<?php

	if( !isset($_COOKIE["username"]) ){
		header("Location:login.php");
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
	
	include_once 'json_function.php';
	$ini_array = parse_ini_file("config/config.ini");
	
	$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
	mysqli_set_charset($conn, "utf8");
	ini_set('default_charset', "UTF-8");
	$appointmentNo = mysqli_real_escape_string($conn,$_POST["appointmentNo"]);
	$patientNo = mysqli_real_escape_string($conn,$_POST["patientNo"]);
	$clinicNo = mysqli_real_escape_string($conn, $_POST["clinicNo"]);
	
	$positionInQueue = mysqli_real_escape_string($conn,$_POST["positionInQueue"]);
	$isUnsigned = mysqli_real_escape_string($conn,$_POST["isUnsigned"]);
	$appointmentDate = mysqli_real_escape_string($conn,$_POST["appointmentDate"]);
	$appointmentTime = mysqli_real_escape_string($conn,$_POST["appointmentTime"]);
	$unsignedType = mysqli_real_escape_string($conn,$_POST["unsignedType"]);
	$scannerFile1 = mysqli_real_escape_string($conn,$_POST["scannerFile1"]);
	$scannerFile2 = mysqli_real_escape_string($conn,$_POST["scannerFile2"]);
	$scannerFile3 = mysqli_real_escape_string($conn,$_POST["scannerFile3"]);
	$scannerFile4 = mysqli_real_escape_string($conn,$_POST["scannerFile4"]);
	$scannerFile5 = mysqli_real_escape_string($conn,$_POST["scannerFile5"]);
	
	
	
	
	
	$operateSql;
	$isFirstVisit = 1;
	if( $appointmentNo == 0 ){
		
		$getIsFirstVisitSql = "select isFirstVisit from patient where patient_no=".$patientNo;
		$isFirstVisitResult = mysqli_query($conn, $getIsFirstVisitSql);
		if( $row = mysqli_fetch_array($isFirstVisitResult)){
			$isFirstVisit = $row["isFirstVisit"];
		}
		
		
		$field =  "clinicNo, patient_no,  position_in_queue, isFirstVisit, scanner_file_1, scanner_file_2, scanner_file_3, scanner_file_4, scanner_file_5, type";
		$value = $clinicNo.",".$patientNo.",".$positionInQueue.",".$isFirstVisit.",'".$scannerFile1."', '".$scannerFile2."', '".$scannerFile3."', '".$scannerFile4."','".$scannerFile5."',";
		if( $isUnsigned == 0 ){
			$field .= ", appointment_datetime, sub_type";
			$value .= "2, str_to_date('".$appointmentDate." ".$appointmentTime."','%d/%m/%Y %H:%i'), 0";
		}else{
			$field .= ", sub_type";
			$value .= "1,".$unsignedType;
		}
		$operateSql = "insert into appointment(".$field.")values(".$value.")";

		
	}else{
		$getFilePathSql = "select scanner_file_1, scanner_file_2, scanner_file_3, scanner_file_4, scanner_file_5, type, sub_type from appointment where appointment_no=".$appointmentNo;
		$getFilePathResult = mysqli_query($conn, $getFilePathSql);
		$oldScannerFile = "";
		$type = 2;
		$sub_type = 0;
		if( $row = mysqli_fetch_array($getFilePathResult)){
			$oldScannerFile1 = $row["scanner_file_1"];
			$oldScannerFile2 = $row["scanner_file_2"];
			$oldScannerFile3 = $row["scanner_file_3"];
			$oldScannerFile4 = $row["scanner_file_4"];
			$oldScannerFile5 = $row["scanner_file_5"];
			/*
			if( strcmp($oldScannerFile1, $scannerFile1) == 0 && file_exists($oldScannerFile1) ){
				unlink($oldScannerFile1);
			}
			if( strcmp($oldScannerFile2, $scannerFile2) == 0 && file_exists($oldScannerFile2) ){
				unlink($oldScannerFile2);
			}
			if( strcmp($oldScannerFile3, $scannerFile3) == 0 && file_exists($oldScannerFile3) ){
				unlink($oldScannerFile3);
			}
			if( strcmp($oldScannerFile4, $scannerFile4) == 0 && file_exists($oldScannerFile4) ){
				unlink($oldScannerFile4);
			}
			if( strcmp($oldScannerFile5, $scannerFile5) == 0 && file_exists($oldScannerFile5) ){
				unlink($oldScannerFile5);
			}
			*/
			$type = $row["type"];
			$sub_type = $row["sub_type"];

		}
		$operateSql = "update appointment set Patient_no=".$patientNo.", clinicNo=".$clinicNo.",scanner_file_1='".$scannerFile1."',scanner_file_2='".$scannerFile2."',scanner_file_3='".$scannerFile3."',scanner_file_4='".$scannerFile4."',scanner_file_5='".$scannerFile5."',";

		if( $isUnsigned == 0 ){
			$appointmentDate = $_POST["appointmentDate"];
			//echo $appointmentDate;
		
			$appointmentTime = $_POST["appointmentTime"];
			//echo $appointmentTime;
			$operateSql = $operateSql."appointment_datetime=str_to_date('".$appointmentDate." ".$appointmentTime."','%d/%m/%Y %H:%i'),sub_type=0, type=".$type;
				
		}else{
			$operateSql = $operateSql."appointment_datetime=null,sub_type=".$unsignedType.", type=1 ";
		}
		
		$operateSql .= " where appointment_no=".$appointmentNo;
	}
	
	//echo $operateSql;
	
	$result = mysqli_query($conn, $operateSql);
	
	$operateType = "Update";
	if( $appointmentNo == 0 && $result ){
		$appointmentNo = mysqli_insert_id($conn);
		$operateType = "Add";

		$updateFirstVisitSql = "update patient set isFirstVisit=0 where patient_no=".$patientNo;
		$updatePatientResult = mysqli_query($conn, $updateFirstVisitSql);
	}
	
	$data = "{\"data\":{";
	if( $result ){
		$data .= "\"status\":\"success\",\"appointmentNo\":\"".$appointmentNo."\",\"type\":\"".$operateType."\"";
	}else{
		$data .= "\"status\":\"fail\",\"msg\":\"".mysqli_error($conn)."\",\"type\":\"".$operateType."\"";
	}
	$data .= "}}";
	echo JsonCharFilter($data);


		

?>
